import time
from pathlib import Path
from loguru import logger
from django.conf import settings

log_path = Path(settings.LOG_PATH)


def getLogger(name):
    """ 使用方式
    from utils.logger import getLogger


    logger = getLogger('auth')

    class RegisterView(APIView):
        authentication_classes = []

        def post(self, request):
            ...
            logger.info('用户: %s 注册成功', username)
            ...

    """
    log_path_info = log_path.joinpath(f'{name}_{time.strftime("%Y-%m-%d")}.log')
    # 日志简单配置 文件区分不同级别的日志
    logger.add(log_path_info,
               rotation=settings.LOGGER.get('maxBytes'),
               encoding='utf-8',
               enqueue=True,
               level=settings.LOGGER.get('level'),
               retention=settings.LOGGER.get('retention'),
               compression=settings.LOGGER.get('compression'))

    return logger
